<template>
  <div class="xtx-goods-page" v-if="detail.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator=">">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ detail.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ detail.categories[0].name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <goods-image :pictures="detail.mainPictures" />
          <GoodsSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsName />
          <GoodsSku :goods="detail" @change="selectSku" v-if="detail" />
          <XtxNumbox :max="detail.inventory" v-model="buynum" />
          <XtxButton
            type="primary"
            style="margin-top: 20px"
            size="large"
            @click="addGoods"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <img
              style="width: 100%"
              v-for="(imgurl, index) in detail.details.pictures"
              alt=""
              :key="index"
              v-imgLazy="imgurl"
            />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import XtxBread from '@/components/Bread'
import XtxBreadItem from '@/components/Bread/item'
import { useRoute } from 'vue-router'
import { provide, ref } from 'vue-demi'
import GoodsImage from './components/goods-img.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import { findGoods } from '@/api/goods'
import { useStore } from 'vuex'
import msg from '@/components/Message'
export default {
  name: 'XtxGoodsPage',
  components: {
    XtxBread,
    XtxBreadItem,
    GoodsImage,
    GoodsSales,
    GoodsName
  },
  setup () {
    const store = useStore()
    const buynum = ref(1)
    const detail = ref({})
    const route = useRoute()
    const currentSku = ref(null)
    provide('detail', detail)
    // 获取商品信息
    const getDetail = async () => {
      const { result } = await findGoods(route.params.id)
      console.log('商品详情', result)
      detail.value = result
    }
    getDetail()
    // 选sku
    const selectSku = (skuinfo) => {
      console.log(skuinfo)
      if (skuinfo.skuId) {
        detail.value.price = skuinfo.price
        detail.value.oldPrice = skuinfo.price
        detail.value.inventory = skuinfo.inventory
        currentSku.value = skuinfo
      } else {
        currentSku.value = null
      }
    }
    // 添加购物车
    const addGoods = () => {
      // 判断sku
      if (!currentSku.value) {
        return msg({ text: '请添加sku' })
      }
      // 判断库存
      if (currentSku.value.inventory === 0) {
        return msg({ text: '商品售罄' })
      }

      // 添加购物车
      const goodsInfo = {
        id: detail.value.id,
        name: detail.value.name,
        picture: detail.value.mainPictures[0],
        skuId: currentSku.value.skuId, // sku属性ID（唯一）
        price: currentSku.value.oldPrice, // 价格
        nowPrice: currentSku.value.price,
        attrsText: currentSku.value.specsText,
        stock: currentSku.value.inventory, // 库存
        selected: true, // 是否在购物车选中
        isEffective: true, // 是否是有效商品
        count: buynum.value // 购买数量
      }
      store.dispatch('cart/addGoodsAction', goodsInfo)
      msg({ text: '添加购物车成功', type: 'success' })
    }

    return { detail, selectSku, buynum, addGoods }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
